package cn.com.blueInfo.bpm.common.enums;

import cn.com.blueInfo.utils.error.BusinessException;

public interface BaseEnum<T> {

    T getCode();

    String getDesc();

    // 根据code反查枚举（由枚举类实现）
    static <E extends Enum<E> & BaseEnum<T>, T> E getByCode(Class<E> enumType, T code) {
        for (E e : enumType.getEnumConstants()) {
            if (e.getCode().equals(code)) {
                return e;
            }
        }
        // 可根据业务需求返回默认值或抛出异常
        throw new BusinessException(500, "无效的code：" + code + "，枚举类型：" + enumType.getName());
    }

    static <E extends Enum<E> & BaseEnum<T>, T> E getByName(String name, Class<E> enumType) {
        try {
            return Enum.valueOf(enumType, name); // 直接通过name匹配枚举
        } catch (IllegalArgumentException e) {
            throw new BusinessException(500, "无效的枚举name：" + name + "，类型：" + enumType.getName());
        }
    }

}
